﻿using System.Collections.Generic;
using System.Web.Configuration;

namespace VisioERLeveller.Model {

    public enum NodeType {
        Normal = 0,
        Relation = 1,
    }

    public class Node : GraphItem {

        public Node(Graph graph, string id, string name, string text) : base(graph, id, name, text) { }

        public NodeType Type { get; private set; }

        public HashSet<string> Links { get; } = new();

        public Node CopyTo(Graph graph) {
            Node node = new(graph, this.ID, this.Name, this.Text);
            node.Tag(this._Tag);
            node.Type = this.Type;
            foreach (var linkId in this.Links)
                node.Links.Add(linkId);
            return node;
        }

        public void AddLink(Link link) {
            this.Links.Add(link.ID);
        }

        public void RemoveLink(Link link) {
            this.Links.Remove(link.ID);
        }

        public bool IsRooted {
            get {
                foreach (var linkId in this.Links) {
                    var link = this.Graph.GetLink(linkId);
                    if (link.FromId != this.ID) continue;
                    if (link.ToId == this.ID) continue;
                    return false;
                }
                return true;
            }
        }

        public bool IsLeaf {
            get {
                foreach (var linkId in this.Links) {
                    var link = this.Graph.GetLink(linkId);
                    if (link.ToId != this.ID) continue;
                    if (link.FromId == this.ID) continue;
                    return false;
                }
                return true;
            }
        }

        public override string ToString() {
            var name = string.IsNullOrWhiteSpace(this.Name) ? "{NONAME}" : this.Name;
            var text = string.IsNullOrWhiteSpace(this.Text) ? string.Empty : $"({this.Text})";
            return $"NODE[{this.ID}]: {name}{text}";
        }

    }
}
